import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => ({
    username: '',
    avatarUrl: '',
    roleType: '',
    email:'',
    birthday:'',
    sex:'',
    introduce:'',
    phone:'',
  }),
  actions: {
    loadUserFromToken() {
      const token = localStorage.getItem('token');
        if (token) {
            try {
                const payload = JSON.parse(decodeURIComponent(atob(token)));
                this.username = payload.username;
                this.avatarUrl = payload.avatarUrl;
                this.email = payload.email;
                this.birthday = payload.birthday;
                this.sex = payload.sex;
                this.introduce = payload.introduce;
                this.phone = payload.phone;
                this.roleType = payload.roleType;

            } catch (error) {
                console.error("解析 token 失败:", error);
                // 清除无效的 token
                localStorage.removeItem('token');
            }
        } else {
            console.log("没有找到 token");
        }
    },


    setUserData(userData) { // 更新UserData
      this.email = userData.email;
      this.birthday = userData.birthday;
      this.sex = userData.sex;
      this.introduce = userData.introduce;
      this.phone = userData.phone;

      // 将最新的用户数据编码为 token，并存储到 localStorage
      const updatedUserInfo = {
        username: this.username,
        avatarUrl: this.avatarUrl,
        email: this.email,
        birthday: this.birthday,
        sex: this.sex,
        introduce: this.introduce,
        phone: this.phone,
        roleType: this.roleType,
      };
      const updatedToken = btoa(encodeURIComponent(JSON.stringify(updatedUserInfo)));
      localStorage.setItem('token', updatedToken);
    },


    clearUserData() { // 清空UserData
      this.avatarUrl = '';
      this.email = '';
      this.birthday = '';
      this.sex = '';
      this.introduce = '';
      this.phone = '';
      localStorage.removeItem('token');

      // 将最新的用户数据编码为 token，并存储到 localStorage
      const updatedUserInfo = {
        username: this.username,
        avatarUrl: this.avatarUrl,
        email: this.email,
        birthday: this.birthday,
        sex: this.sex,
        introduce: this.introduce,
        phone: this.phone,
        roleType: this.roleType,
      };
      const updatedToken = btoa(encodeURIComponent(JSON.stringify(updatedUserInfo)));
      localStorage.setItem('token', updatedToken);
    },


    updateAvatar(avatarUrl) { // 更新Avatar
      this.avatarUrl = avatarUrl;
      console.log("调用updateAvatar:", avatarUrl);

      // 将最新的用户数据编码为 token，并存储到 localStorage
      const updatedUserInfo = {
        username: this.username,
        avatarUrl: this.avatarUrl,
        email: this.email,
        birthday: this.birthday,
        sex: this.sex,
        introduce: this.introduce,
        phone: this.phone,
        roleType: this.roleType,
      };
      console.log("调用updateAvatar:", updatedUserInfo.avatarUrl);
      const updatedToken = btoa(encodeURIComponent(JSON.stringify(updatedUserInfo)));
      localStorage.setItem('token', updatedToken);
    },
  },
});
